package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.service.AddressBookService;
import org.apache.commons.math3.analysis.function.Add;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;
    /**
     * 新增收货地址
     *
     * @param addressBook
     * @return
     */
    @Override
    public Boolean addAddressBook(AddressBook addressBook) {
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setUserId(addressBook.getCreateUser());
        addressBook.setIsDefault(0);
        return addressBookMapper.addAddressBook(addressBook)>0;
    }

    /**
     * 显示用户地址列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<AddressBook> findAllAddress(Long userId) {
        return addressBookMapper.findAllAddress(userId);
    }

    /**
     * 根据id修改默认地址
     *
     * @param
     * @return
     */
    @Override
    public Boolean updateAddress(AddressBook addressBook) {
        return addressBookMapper.updateAddress(addressBook)>0;
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @Override
    public Boolean setDefaultAddress(AddressBook addressBook) {
        //全部设置为非默认地址
        Long userId = addressBook.getUserId();
        addressBookMapper.updateDefualt(userId);
        //根据选中的id设置默认地址
        addressBookMapper.setDefaultAddress(addressBook);
        return true;
    }

    /**
     * 数据回显：回显地址
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook findAddressById(Long id) {
        return addressBookMapper.findAddressById(id);
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @Override
    public Integer deleteAddressBook(Long id) {
        return addressBookMapper.deleteAddressBook(id);
    }


    /**
     * //得到默认地址
     * @param currentId
     * @return
     */
    @Override
    public AddressBook getDefaultAddress(Long currentId) {
        return addressBookMapper.getDefaultAddress(currentId);
    }
}
